from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
import netifaces


class LoginRequiredMixin(object):

    @method_decorator(login_required(login_url='/user/login'))
    def dispatch(self, request, *args, **kwargs):
        return super(LoginRequiredMixin, self).dispatch(request, *args, **kwargs)


def get_current_ip():
    '''
    Using netifaces to fetch the ip address of the current machine.
    '''
    ips = []
    exclude_iface = ['lo']
    interfaces = netifaces.interfaces()
    for iface in interfaces:
        if iface not in exclude_iface:
            if netifaces.AF_INET in netifaces.ifaddresses(iface):
                addrs = netifaces.ifaddresses(iface)[netifaces.AF_INET] # mac addr: netifaces.AF_LINK
                for addr in addrs:
                    ips.append(addr['addr'])
    return ips
